package com.wisemoresystem.seal.action.seal.install;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_seal;
import com.wisemoresystem.seal.model.t_seal_new;
import com.wisemoresystem.seal.model.t_seal_user;
import com.wisemoresystem.seal.model.t_seal_user_use;
import com.wisemoresystem.seal.service.MacService;
import com.wisemoresystem.seal.service.SealSetService;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 印章安装
 * 
 * @author Administrator
 *
 */
@Controller("ixSealInstall")
@Scope("prototype")
@RequestMapping("/ix/seal/set/install")
public class SealInstallAction extends ProjBaseAction {

	@Autowired
	private SealSetService sealService;
	@Autowired
	private MacService macService;

	/**
	 * 可以被安装的印控仪
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> list = sealService.selectSealInstallList(
				srh_name, page);

		ModelAndView mv = new ModelAndView("/jsp/seal/set/install/list");
		mv.addObject("list", list);
		mv.addObject("user", WebTools.getCurrentUser());
		return mv;
	}

	/**
	 * 印章的详细信息
	 * 
	 * @param seal_id
	 *            印章id
	 * @return
	 */
	@RequestMapping("/edit_form")
	public ModelAndView edit_form(String seal_id) {

		List<Map<String, Object>> maclist = sealService.selectSealMacList();
		List<Map<String, Object>> userlist = sealService.selectSealUserList();
		t_seal seal = oc.findObjById(seal_id, t_seal.class);
		t_bank bank = oc.findObjById(WebTools.getCurrentBankId(), t_bank.class);
		t_seal_new seal_new = oc.findObjById(seal.getSeal_new_id(),
				t_seal_new.class);

		ModelAndView mv = new ModelAndView("/jsp/seal/set/install/add_form");
		mv.addObject("seal", seal);
		mv.addObject("bank", bank);
		mv.addObject("seal_new", seal_new);
		mv.addObject("maclist", maclist);
		mv.addObject("userlist", userlist);
		return mv;
	}

	/**
	 * 印章安装
	 * 
	 * @param seal_use
	 *            印章安装卸载信息
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(t_seal_user_use seal_use) {

		if ("success".equals(macService.checkUniqueLocation(
				seal_use.getMac_id(), seal_use.getPosition()))) {
			super.setTipMsg("该印控仪的该位置已使用,请重新选择！", Tip.Type.warning);
			ModelAndView mv = new ModelAndView("redirect:edit_form.htm");
			mv.addObject("seal_id", seal_use.getSeal_id());
			return mv;
		}
		t_seal_user_use user_user = sealService.selectGetSealUserUse(seal_use
				.getSeal_id());
		if (ChkTools.isNotNull(user_user)) {
			super.setTipMsg("对不起，印章已安装！", Tip.Type.success);
		} else {
			t_seal_user seal_user = sealService.selectGetSealUser(seal_use
					.getSeal_id());
			t_seal seal = oc.findObjById(seal_use.getSeal_id(), t_seal.class);
			seal_use.setBank_id(WebTools.getCurrentBankId());
			seal_use.setTime_in(new Timestamp(System.currentTimeMillis()));
			seal_use.setUser_id(WebTools.getCurrentUserId());
			seal_use.setSeal_user_id(seal_user.get_id());
			seal_use.setUse_user_id(seal_user.getUse_user_id());
			seal_use.setFlag("已安装");
			oc.saveObj(seal_use);

			seal_user.setFlag("使用中");
			oc.updateObj(seal_user);
			seal.setMac_id(seal_use.getMac_id());
			seal.setFlag_use("使用中");
			seal.setFlag_use_status("已安装");
			oc.updateObj(seal);
			logT.logH("印章安装", t_seal_user.class, seal_user.get_id());
			super.setTipMsg("安装成功！", Tip.Type.success);
		}

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	/**
	 * 卸载
	 * 
	 * @param seal_id
	 *            印章id
	 * @throws IOException
	 */
	@RequestMapping("/operate")
	public void operate(String seal_id) throws IOException {
		resp.setCharacterEncoding("utf-8");

		t_seal_user_use user_user = sealService.selectGetSealUserUse(seal_id);
		if (ChkTools.isNull(user_user)) {
			resp.getWriter().print("对不起，印章已卸载！");
			resp.getWriter().flush();
		} else {
			t_seal_user_use seal_use = sealService
					.selectGetSealUserUse(seal_id);
			seal_use.setTime_out(new Timestamp(System.currentTimeMillis()));
			seal_use.setFlag("已卸载");
			oc.updateObj(seal_use);

			t_seal_user seal_user = oc.findObjById(seal_use.getSeal_user_id(),
					t_seal_user.class);
			t_seal sealDb = oc.findObjById(seal_id, t_seal.class);
			sealDb.setFlag_use_status("未安装");
			if ("机构已改动，请处理印章".equals(sealDb.getFlag_change())) {
				sealDb.setFlag_use("待归还");
			} else {
				sealDb.setFlag_use("已领取");
			}
			oc.updateObj(sealDb);

			seal_user.setFlag("已领取");
			oc.updateObj(seal_user);

			logT.logH("印章卸载", t_seal_user.class, seal_user.get_id());
			resp.getWriter().print("卸载成功！");
			resp.getWriter().flush();
		}

	}

	@RequestMapping("/unique_code")
	public void unique_code(String mac_id, String position) throws IOException {

		String msg = macService.checkUniqueLocation(mac_id, position);

		resp.getWriter().print(msg);
		resp.getWriter().flush();
	}

}
